<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
          >批量禁用</el-button
        >
        <el-button
          type="success"
          icon="el-icon-plus"
          class="handle-del mr10"
          @click="addOne"
          >增加</el-button
        >
        <el-input
          v-model="query.queryParam.courseName"
          placeholder="课程名"
          class="handle-input mr10"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >搜索</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          :selectable="checkSelectable"
        ></el-table-column>
        <el-table-column
          prop="courseName"
          label="课程名"
          width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="teacherName"
          label="授课老师"
          align="center"
        ></el-table-column>
        <el-table-column prop="belongYear" label="所属学年" align="center"></el-table-column>
        <el-table-column prop="belongTerm" label="所属学期" align="center"></el-table-column>
        <el-table-column
          prop="fkClassName"
          label="挂靠班级"
          align="center"
        ></el-table-column>
        <el-table-column prop="remarks" label="备注" align="center"></el-table-column>
        <el-table-column label="操作" width="130" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="total"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="500px">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="课程名" prop="courseName">
          <el-input type="text" v-model="form.courseName"></el-input>
        </el-form-item>
        <el-form-item label="授课老师" prop="teacherName">
          <el-input type="text" v-model="form.teacherName"></el-input>
        </el-form-item>
        <el-form-item label="挂靠班级" prop="fkClassId">
          <el-select v-model="form.fkClassId" placeholder="请选择">
            <el-option
              v-for="item in classes"
              :key="item.id"
              :value="item.id"
              :label="item.className"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属学年" prop="belongYear">
          <el-input type="number" v-model="form.belongYear"></el-input>
        </el-form-item>
        <el-form-item label="所属学期" prop="belongTerm">
          <el-select v-model="form.belongTerm" placeholder="请选择">
            <el-option value="第一学期"></el-option>
            <el-option value="第二学期"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks"
          ><el-input type="text" v-model="form.remarks"></el-input
        ></el-form-item>
        <el-form-item label="联系方式" prop="teacherPhone">
          <el-input
            type="text"
            v-model="form.teacherPhone"
            placeholder="联系方式仅用于确定授课老师信息，不会保存该信息"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="btnBoolean" @click="saveEdit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
var _this;
export default {
  name: "basetable",
  data() {
    return {
      query: {
        queryParam: {
          className: "",
        },
        pageIndex: 1,
        pageSize: 10,
      },
      tableData: [],
      // 班级
      classes: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      total: 0,
      form: {},
      rowdata: {},
      btnBoolean: true,
      rules: {
        courseName: [{ required: true, message: "课程名不能为空", trigger: "blur" }],
        teacherName: [{ required: true, message: "授课老师不能为空", trigger: "blur" }],
        fkClassId: [{ required: true, message: "挂靠班级不能为空", trigger: 'change' }],
        belongYear: [{ required: true, message: "所属学年不能为空", trigger: "blur" }],
        belongTerm: [
          { required: true, message: "所属学期不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getData();
    this.getClasses();
    _this = this;
  },
  methods: {
    // 获取数据
    getData() {
      this.$post("/course/courselist", this.query).then((res) => {
        if (res.code === 0) {
          this.tableData = res.data.records;
          this.total = res.data.total || 1;
        }
      });
    },
    // 获取班级
    getClasses(){
      this.$post("/class/getall", null).then((res) => {
        if (res.code === 0) {
          this.classes = res.data
        }
      });
    },
    // 是否可选中
    checkSelectable(row) {
      return row.isDel === 0;
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.delList.push(row.id);
          this.deleteList(this.delList);
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      this.delList = this.multipleSelection.map((x) => {
        return x.id;
      });
      this.deleteList(this.delList);
      this.multipleSelection = [];
    },
    //删除操作
    deleteList(param) {
      this.$post("/course/del", param).then((res) => {
        if (res.code === 0) {
          this.$message.success("删除成功");
          this.getData();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    // 新增操作
    addOne() {
      this.form = {};
      this.editVisible = true;
    },
    // 编辑操作
    handleEdit(index, row) {
      this.rowdata = JSON.parse(JSON.stringify(row));
      if (row.remarks === null || row.remarks === undefined) {
        this.rowdata.remarks = "";
      }
      this.idx = index;
      this.form = JSON.parse(JSON.stringify(this.rowdata));
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$post("/course/insertorupdate", this.form).then((res) => {
            if (res.code === 0) {
              this.$message.success(res.msg);
              this.getData();
              this.editVisible = false;
              this.form = {};
              this.rowdata = {};
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("输入错误！");
        }
      });
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
  },
  watch: {
    form: {
      handler: (val, oldval) => {
        if (JSON.stringify(_this.form) !== JSON.stringify(_this.rowdata)) {
          _this.btnBoolean = false;
        } else {
          _this.btnBoolean = true;
        }
      },
      deep: true,
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 170px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
/deep/.el-upload {
  width: 150px;
  height: 210px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 210px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 210px;
  display: block;
}
/* 谷歌 */
/deep/input::-webkit-outer-spin-button,
/deep/input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
}
/* 火狐 */
/deep/input {
  -moz-appearance: textfield;
}
</style>
